package com.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

	static String filepath = ReadFile.class.getClassLoader().getResource("read.txt").getFile();

	// 1. 单字节读取文件
	public static void readFileAsByte() throws IOException {
		java.io.InputStream is = null;
		try {
			is = new FileInputStream(filepath);
			int data = -1;
			while ((data = is.read()) != -1) {// -1 表示读取到达文件结尾
				// 操作数据
				System.out.print((byte) data + " ");
			}
		} finally {
			if (is != null) {
				is.close();// 关闭流
			}
		}
	}

	// 2. 字节数组读取文件
	public static void readFileAsByteArray() throws IOException {
		java.io.InputStream is = null;
		try {
			is = new BufferedInputStream(new FileInputStream(filepath));// 组装BufferedInputStream流，加入缓冲能力
			byte[] data = new byte[256];
			int len = -1;
			while ((len = is.read(data)) != -1) {// -1 表示读取到达文件结尾
				// 操作数据
				for (int i = 0; i < len; i++) {
					System.out.print(data[i] + " ");
				}
			}
		} finally {
			if (is != null) {
				is.close();// 关闭流
			}
		}
	}

	// 3. 字符流读取单个字符
	public static void readFileAsChar() throws IOException {
		java.io.Reader r = null;
		try {
			r = new FileReader(filepath);
			int data = -1;
			while ((data = r.read()) != -1) {// -1 表示读取到达文件结尾
				// 操作数据
				System.out.print((char) data);
			}
		} finally {
			if (r != null) {
				r.close();// 关闭流
			}
		}
	}

	// 4. 字符流读取字符数组
	public static void readFileAsCharArray() throws IOException {
		java.io.Reader r = null;
		try {
			r = new BufferedReader(new FileReader(filepath));// 组装BufferedReader流，加入缓冲能力
			char[] data = new char[256];
			int len = -1;
			while ((len = r.read(data)) != -1) {// -1 表示读取到达文件结尾
				// 操作数据
				for (int i = 0; i < len; i++) {
					System.out.print(data[i]);
				}
			}
		} finally {
			if (r != null) {
				r.close();// 关闭流
			}
		}
	}

	public static void main(String[] args) throws IOException {
		// readFileAsByte();
		// readFileAsByteArray();
		// readFileAsChar();
		readFileAsCharArray();

	}
}
